package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if ("admin".equals(username) && "123".equals(password)) {
            //登录成功，跳转
            //请求重定向和请求转发不同
            //请求重定向可以跳转到服务器外部的站点、资源
            //请求重定向地址栏会改变
            //不能共享request域对象
            //请求重定向是两次请求1.发送给当前Servlet
            //2.拿到网址，浏览器再次去访问
            resp.sendRedirect("http://www.baidu.com");
        } else {
            //登录失败，跳回登录页面
            resp.sendRedirect("http://localhost:8083/demo0914_01/login.html");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
